package com.stylefeng.guns.modular.system.task;

import java.net.URI;
import java.net.URL;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.model.Djg;
import com.stylefeng.guns.modular.system.model.Gpdata;
import com.stylefeng.guns.modular.system.service.IDjgService;
import com.stylefeng.guns.modular.system.service.IGpdataService;

@Service
public class DjgImportTask {

	private Logger log = LoggerFactory.getLogger(DjgImportTask.class);

	private static final HttpClientUtil clientUtil = new HttpClientUtil();
	
	@Autowired
	private IDjgService djgService;
	
	@Autowired
    private IGpdataService gpdataService;
	
	public void execute(boolean isHoliday) {
		List<Gpdata> gpdatas =  gpdataService.selectList(null);
		for(Gpdata gpdate : gpdatas){
			String url = ImportContants.getDJGDataPathStr(1,gpdate.getgDm());
			HttpGet httpGet = HttpClientUtil.getGetRequest();
			try {
				URL http = new URL(url);
				URI uri = new URI(http.getProtocol(), http.getHost(), http.getPath(), http.getQuery(), null);
				httpGet.setURI(uri);
				String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
				JSONObject jsonObj = JSON.parseObject(json);
				if(jsonObj.getIntValue("pages") > 0){
					parseDjgData(jsonObj.getString("data"));
					int pages = jsonObj.getIntValue("pages");
					for(int i= 2; i <= pages ; i++){
						url = ImportContants.getDJGDataPathStr(i ,gpdate.getgDm());
						http = new URL(url);
						uri = new URI(http.getProtocol(), http.getHost(), http.getPath(), http.getQuery(), null);
						httpGet.setURI(uri);
						json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
						jsonObj = JSON.parseObject(json);
						parseDjgData(jsonObj.getString("data"));
					}
				}
			} catch (Exception e) {
				log.error("get djg data is error!" + e);
			}finally{
				httpGet.abort();
			}
		}
	}
	private void parseDjgData(String data) {
		String[] djgs = StringUtils.substringsBetween(data, "\"", "\"");
		for(String djg : djgs){
			try{
				if(StringUtils.isNotEmpty(djg)){
					String[] detail = StringUtils.split(djg,",");
					Djg newDjg = new Djg();
					newDjg.setgName(detail[1]);
					newDjg.setgPtype(detail[4]);
					newDjg.setgDate(DateUtil.parse(detail[5], "yyyy-MM-dd"));
					newDjg.setgPtype(detail[4]);
					newDjg = djgService.selectByDate(newDjg);
					if(newDjg == null){
						newDjg = new Djg();
						newDjg.setgName(detail[1]);
						newDjg.setgPtype(detail[4]);
						newDjg.setgDate(DateUtil.parse(detail[5], "yyyy-MM-dd"));
						newDjg.setgPtype(detail[4]);
						newDjg.setgAvgPrice(Double.valueOf(detail[8]));
						newDjg.setgBdgs(Double.valueOf(detail[6]));
						newDjg.setgBdhgs(Double.valueOf(detail[7]));
						newDjg.setgBdmoney(Double.valueOf(detail[13]));
						newDjg.setgBdtype(detail[12]);
						newDjg.setgDjggx(detail[10]);
						newDjg.setgDm(detail[2]);
						newDjg.setgJob(detail[14]);
						djgService.insert(newDjg);
					}
				}
			}catch(Exception e){
				log.error("parse error!" ,e);
			}
		}
	}
}
